// pages/user/recommend/recommend.js
const T = getApp().globalData.T
const config = getApp().globalData.config
let token= '';
let userId = '';

import {
  $wuxSelect,
  $wuxCalendar
} from '../../../wux-components/index'
import uploadImg from '../../../utils/uploadImg.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    uploadUrl: config.host + 'admin/file/singleFile',
    popularizeDate: '请选择',
    photoPopularizeDate: '请选择',
    isShowCalendar:false,
    isShowPhotoCalendar:false,
    selectedPopularizeDate:'',
    selectedPhotoPopularizeDate:'',
    isShowPhotoPopularize:false,
    isShowPopularize:false,
    popularizeList:[],
    photoPopularizeList: [],
    templateImg:'/resource/image/add_post_img.png',
    isOpenUploadImg:false,
    companyTopNumSum:0,
    showInCourseSum:0,
    formData: {
      type: 'image'
    },
    fileList: [],
    dates:[],
    photoDates:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    token = getApp().globalData.token;
    userId = getApp().globalData.userId;
    this.getPopularizeCount();
    this.getPopularizelist(1);
    this.getPopularizelist(3);


  },
  onUpdateTemplate() {
    let that = this;
    this.data.isOpenUploadImg = true;
    uploadImg(that, 'image', 'resumeImgUpload')
      .then((res) => {
        that.setData({
          templateImg: res,
          isOpenUploadImg: false
        })
      })
  },
  onPopularizeDate(e) {
    const now = new Date()
    const minDate = now.setDate(now.getDate() );
    const maxDate = now.setDate(now.getDate() + 30);
    let type = e.currentTarget.dataset.type;
    let dates = [];
    if(type == 0){
      this.setData({
        isShowPopularize: true,
        isShowCalendar: true
      });
      dates= this.data.dates;
    }else{
      this.setData({
        isShowPhotoPopularize:true,
        isShowPhotoCalendar: true
        
      });
      dates = this.data.photoDates;
    }
    
    $wuxCalendar().open({
      value: dates || [],
      minDate: minDate,
      maxDate: maxDate,
      multiple: true,
      onChange: (values, displayValues) => {
        this.onShowPopularizeList(values,type);
      },
      onClose: () => {
        if(type == 0){
          this.setData({
            isShowCalendar: false,
            isShowPopularize: false,
          })
        }else{
           this.setData({
          isShowPhotoCalendar: false,
          isShowPhotoPopularize: false
        })
        }
       
      }
    });
  },
  onShowPopularizeList(values,type) {
    if (values.length == 0) return;
    let _popularize = this.data.initPopularizeList
if(type == 0){

   _popularize = this.data.initPopularizeList;
}else{
   _popularize = this.data.initPhotoPopularizeList;
}
    let _newArray = [];
    let _user = [];
    if(type == 0){
      this.setData({dates:values})
    }else{
      this.setData({ photoDates: values })


    }
    values.forEach(m => {
      _popularize.forEach((e, i) => {
        let _time = T.formatTime(e.date);
        let stamp = new Date(_time.year + '/' + _time.month + '/' + _time.day + ' 00:00:00').getTime();
        if (m == stamp) {
          _newArray.push(e);
          if (e.unoccupied != 0) {
            _user.push(e.date);
          }
        }
      });
    })
    if(type == 0){
      this.setData({ popularizeList: _newArray, selectedPopularizeDate: _user });
      console.log('selectedPopularizeDate:' + this.data.selectedPopularizeDate);
    }else{
      this.setData({ photoPopularizeList: _newArray, selectedPhotoPopularizeDate: _user });
      console.log('selectedPhotoPopularizeDate:' + this.data.selectedPhotoPopularizeDate);


    }
    let _str = '';
    _user.forEach(m => {
      let _time = T.formatTime(m);
      let stamp = _time.year + '-' + _time.month + '-' + _time.day;
      _str += stamp + ','
    });
    console.log(_str)
    if(type == 0){
      this.setData({ popularizeDate: '已选'+this.data.dates.length+'天' })

    }else{
      this.setData({ photoPopularizeDate: '已选' + this.data.photoDates.length + '天' })

    }
  },
  confrimCompany(){
    T.req({
      url: config.api + 'popularize/company?token='+token,
      method: 'POST',
      data: {
        userId:userId,
        effectiveDates: this.data.selectedPopularizeDate
      },
      success: (res) => {
        T.toast({
          id: '#toast',
          text: '推广成功'
        });
        this.getPopularizeCount();
      }
    })
  },
  confrimPhoto() {
    T.req({
      url: config.api + 'popularize/carousel?token=' + token,
      method: 'POST',
      data: {
        userId:userId,
        cover:this.data.templateImg,
        showTimes: this.data.selectedPhotoPopularizeDate
      },
      success: (res) => {
        T.toast({
          id: '#toast',
          text: '推广成功'
        }); 
        this.getPopularizeCount()
      }
    })
  },
  onSureSelectPopularize() {
    this.setData({
      isShowCalendar: false,
      isShowPopularize: false,
      isShowPhotoCalendar:false,
      isShowPhotoPopularize:false
    })
  },
  getPopularizeCount(){
    T.req({
      url: config.api + 'popularize/residue',
      method: 'GET',
      data: {
        token: token,
      },
      success: (res) => {
        this.setData({
          companyTopNumSum: res.companyTopNumSum,
          showInCourseSum: res.showInCourseSum
        })
      }

    })


  },
  getPopularizelist(type) {
    T.req({
      url: config.api + 'popularize/list/available',
      method: 'GET',
      data: {
        token: token,
        type: type
      },
      success: (res) => {
        if(type == 1){
          this.data.initPopularizeList = res;
        }else{
          this.data.initPhotoPopularizeList = res;
        }
      }
    })

  },
  
})